import {ref} from 'vue';
import {defineStore} from "pinia";
import {BaseDirectory, readDir, writeTextFile} from "@tauri-apps/plugin-fs";
import {getFileName, isMarkdownFile, listToTree} from "../utils/utils.js";
import {useFileStore} from "./fileStore.js";

export const useSystemStore = defineStore('systemStore', () => {
	// 是否浏览器端
	const isWeb = ref(false);

	const maximize = ref(false);

	// 默认主题
	const defaultMarkdownTheme = ref('');
	// 默认掘金主题
	const defautJuejinTheme = ref('');
	const defaultCodeTheme = ref('');
	const viewDevice = ref('web');
	const isDark = ref(false);
	const copyPlat = ref(['wechat', 'juejin', 'zhihu', 'csdn']);
	// 点击拷贝后是否打开平台
	const openPlat = ref(true);
	// 拷贝到掘金平台方式
	const copyJuejinWay = ref(false);
	// 关闭按钮
	const closeMethod = ref('close');

	// 当前打开的文件路径
	const openDir = ref('');

	// 文件名前缀规则
	const newFileNamePrefix = ref('date');
	// 文件名前缀 自定义文本
	const newFileNamePrefixText = ref('');
	// 插入图片备注文字
	const insertImageAltText = ref('');
	// 插入文章尾部的内容片段
	const insertDownFooterText = ref('');

	/** 初始化设置参数 */
	function initSettings(setting) {
		isWeb.value = !window.__TAURI__;

		defaultMarkdownTheme.value = setting.defaultMarkdownTheme;
		defautJuejinTheme.value = setting.defautJuejinTheme;
		defaultCodeTheme.value = setting.defaultCodeTheme;
		copyJuejinWay.value = setting.copyJuejinWay;
		closeMethod.value = setting.closeMethod;
		copyPlat.value = setting.copyPlat;
		viewDevice.value = setting.viewDevice;
		isDark.value = setting.isDark;
		openDir.value = setting.openDir;

		newFileNamePrefix.value = setting?.newFileNamePrefix || 'date';
		newFileNamePrefixText.value = setting?.newFileNamePrefixText || '';
		insertImageAltText.value = setting?.insertImageAltText || '';
		insertDownFooterText.value = setting?.insertDownFooterText || '';

		// 初始化过程中打开上次打开过的文件，如果没有则中止操作
		initOpenFile(openDir.value);
	}


	/**
	 * 初始化打开文件或文件夹
	 * @param {string} openDir - 要打开的文件或文件夹路径
	 * @returns {void}
	 */
	function initOpenFile(openDir) {
		if (!openDir || openDir === '') return;
		// 判断当前是否为文件/文件夹
		const isFile = isMarkdownFile(openDir);
		const fileStore = useFileStore();
		if (isFile) {
			// 处理单个文件的情况，将文件信息添加到文件树列表中
			const fileName = getFileName(openDir);
			fileStore.setFileTreeList([
				{
					id: '2133',
					label: fileName,
					leaf: true,
					children: null,
					path: openDir
				}
			])
		} else {
			fileStore.setFileRootPath(openDir);
			// 处理文件夹的情况，读取目录内容并转换为树形结构
			readDir(openDir).then(res => {
				const folderTreeData = listToTree(res, openDir);
				fileStore.setFileTreeList(folderTreeData);
			});
		}
	}

	function toggleMaximize() {
		maximize.value = !maximize.value;
	}

	function setMarkdownTheme(theme) {
		defaultMarkdownTheme.value = theme;
		setDefaultSetting('defaultMarkdownTheme', theme);
	}

	function setJuejinTheme(theme) {
		defautJuejinTheme.value = theme;
		setDefaultSetting('defautJuejinTheme', theme);
	}

	function setCodeTheme(theme) {
		defaultCodeTheme.value = theme;
		setDefaultSetting('defaultCodeTheme', theme);
	}

	function setViewDevice(device) {
		viewDevice.value = device;
		setDefaultSetting('viewDevice', device);
	}

	function setIsDark(theme) {
		isDark.value = theme;
		setDefaultSetting('isDark', theme);
	}

	function setOpenPlat(open) {
		openPlat.value = open;
		setDefaultSetting('openPlat', open);
	}

	function setCopyPlat(plat) {
		copyPlat.value = plat;
		setDefaultSetting('copyPlat', plat);
	}

	function setCopyJuejinWay(way) {
		copyJuejinWay.value = way;
		setDefaultSetting('copyJuejinWay', way);
	}

	function setCloseMethod(type) {
		closeMethod.value = type;
		setDefaultSetting('closeMethod', type);
	}

	/** 保存打开的文件路径 */
	function setOpenDir(dir) {
		openDir.value = dir;
		setDefaultSetting('openDir', dir);
	}

	/** 保存文件名前缀规则 */
	function setNewFileNamePrefix(prefix) {
		newFileNamePrefix.value = prefix;
		setDefaultSetting('newFileNamePrefix', prefix);
	}

	/** 保存文件名前缀规则 自定义文本 */
	function setNewFileNamePrefixText(text) {
		newFileNamePrefixText.value = text;
		setDefaultSetting('newFileNamePrefixText', text);
	}

	/** 保存插入图片备注内容 */
	function setInsertImageAltText(text) {
		insertImageAltText.value = text;
		setDefaultSetting('insertImageAltText', text);
	}

	/** 保存插入文章尾部的内容片段 */
	function setInsertDownFooterText(text) {
		insertDownFooterText.value = text;
		setDefaultSetting('insertDownFooterText', text);
	}

	/**
	 * 修改配置信息
	 * @param {string} key - 配置项
	 * @param {string} value - 配置值
	 * */
	async function setDefaultSetting(key, value) {
		const payload = {
			defaultMarkdownTheme: defaultMarkdownTheme.value,
			defautJuejinTheme: defautJuejinTheme.value,
			defaultCodeTheme: defaultCodeTheme.value,
			viewDevice: viewDevice.value,
			isDark: isDark.value,
			openPlat: openPlat.value,
			copyPlat: copyPlat.value,
			copyJuejinWay: copyJuejinWay.value,
			closeMethod: closeMethod.value,
			openDir: openDir.value,
			newFileNamePrefix: newFileNamePrefix.value,
			newFileNamePrefixText: newFileNamePrefixText.value,
			insertImageAltText: insertImageAltText.value,
			insertDownFooterText: insertDownFooterText.value
		}
		payload[key] = value;
		await writeTextFile('setting-conf.json', JSON.stringify(payload), {baseDir: BaseDirectory.AppLocalData});
	}

	return {
		isWeb,
		maximize,
		defaultMarkdownTheme,
		defautJuejinTheme,
		defaultCodeTheme,
		viewDevice,
		isDark,
		openPlat,
		copyPlat,
		copyJuejinWay,
		closeMethod,
		newFileNamePrefix,
		newFileNamePrefixText,
		insertImageAltText,
		insertDownFooterText,
		initSettings,
		toggleMaximize,
		setMarkdownTheme,
		setJuejinTheme,
		setCodeTheme,
		setViewDevice,
		setIsDark,
		setOpenPlat,
		setCopyPlat,
		setCopyJuejinWay,
		setCloseMethod,
		setOpenDir,
		setNewFileNamePrefix,
		setNewFileNamePrefixText,
		setInsertImageAltText,
		setInsertDownFooterText
	};
});